package com.i2works.smartluolong.web.service.user.impl;

import com.google.common.collect.Lists;
import com.i2works.smartluolong.web.service.user.IUserResService;
import com.i2works.smartluolong.utils.basecommon.BaseCommonServiceImpl;
import com.i2works.smartluolong.utils.exception.DAOException;
import com.i2works.smartluolong.utils.exception.ServiceException;
import com.i2works.smartluolong.web.dal.user.dao.IUserResDAO;
import com.i2works.smartluolong.web.entity.user.UserResDO;
import com.i2works.smartluolong.web.entity.user.UserResQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 会员资源表实现类
 *
 * @author Liu Yutao
 * @className UserResController
 * @email koal@vip.qq.com
 * @date 2016/2/26 14:30
 */

@Service("userResService")
public class UserResServiceImpl  extends BaseCommonServiceImpl<UserResQuery,UserResDO> implements IUserResService {

    private final IUserResDAO userResDAO;

    @Autowired
    public UserResServiceImpl(IUserResDAO userResDAO) {
        this.userResDAO = userResDAO;
    }

    /**
     * 根据查询资源列表(树)
     */
    @Override
    public List<UserResDO> queryTreeList(UserResQuery query) throws ServiceException {
        try {
            return userResDAO.queryTreeList(query);
        } catch (DAOException e) {
            throw new ServiceException(e);
        }
    }


    /**
     * 查询某个用户的资源
     */
    @Override
    public Set<UserResDO> queryUserResList(UserResQuery query) throws ServiceException {
        try {
            return userResDAO.queryUserResList(query);
        } catch (DAOException e) {
            throw new ServiceException(e);
        }
    }


    /**
     * 添加系统资源
     */
    @Override
    public void save(UserResDO userResDO) throws ServiceException {
        try {
            if (userResDO.getId() != null && userResDO.getId() != 0) {
                userResDAO.update(userResDO);
            } else {
                userResDO.setCreateName(userResDO.getUpdateName());
                userResDAO.save(userResDO);
            }
        } catch (DAOException e) {
            throw new ServiceException(e);
        }
    }

    /**
     * 某人所拥有的资源ID
     */
    @Override
    public UserResDO queryHasChecked(UserResQuery query) throws ServiceException {
        try {
            return userResDAO.queryHasChecked(query);
        } catch (DAOException e) {
            throw new ServiceException(e);
        }
    }


    @Override
    public void updateStatus(UserResDO userResDO) throws ServiceException {
        try {
            if (null != userResDO) {
                userResDAO.update(userResDO);
                //如果是父类，关联设置其子类状态
                if (userResDO.getPid() == 0) {
                    UserResQuery query = new UserResQuery();
                    query.setPid(userResDO.getId());
                    //查询其所有子类
                    List<UserResDO> childrenResList = userResDAO.queryAllList(query);
                    //循环输出子类，并设置其status为父类status
                    for (UserResDO childrenRes : childrenResList) {
                        childrenRes.setUpdateName(userResDO.getUpdateName());
                        childrenRes.setStatus(userResDO.getStatus());
                        //更新子类
                        userResDAO.update(childrenRes);
                    }
                }
            }

        } catch (DAOException e) {
            throw new ServiceException(e);
        }
    }

    /**
     * 资源下拉菜单
     */
    @Override
    public List<UserResDO> queryResSelect(UserResQuery query) throws ServiceException {
        try {
            List<UserResDO> resSelect = userResDAO.queryList(query);
            List<UserResDO> formatSelect = Lists.newArrayList();
            for (UserResDO adminResDO : resSelect) {
                if(adminResDO.getResType() == 1){
                    formatSelect.add(adminResDO);
                    UserResQuery adminResQuery = new UserResQuery();
                    adminResQuery.setPid(adminResDO.getId());
                    adminResQuery.setResType(2);
                    List<UserResDO> subResList = userResDAO.queryList(adminResQuery);
                    if(subResList.size() > 0){
                        formatSelect.addAll(subResList);
                    }
                }
            }
            return formatSelect;
        } catch (DAOException e) {
            throw new ServiceException(e);
        }
    }

    @Override
    public void updateBatchStatus(Map map) throws ServiceException {
        try {
            userResDAO.updateBatchStatus(map);
        } catch (DAOException e) {
            throw new ServiceException(e);
        }
    }

    @Override
    public void delete(Integer id) throws ServiceException {
        try {
            userResDAO.delete(id);
            userResDAO.deleteRoleRes(id);
        } catch (DAOException e) {
            throw new ServiceException(e);
        }
    }

    @Override
    public void deleteBatch(List<Integer> idsList) throws ServiceException {
        try {
            userResDAO.deleteBatch(idsList);
            userResDAO.deleteBatchRoleRes(idsList);
        } catch (DAOException e) {
            throw new ServiceException(e);
        }
    }
}
